package com.icard.wx.icardwx.controller;


import com.dsbj.common.msg.ResponseMessage;
import com.icard.wx.icardwx.model.*;
import com.icard.wx.icardwx.service.IUserService;
import com.icard.wx.icardwx.service.IWxService;
import com.icard.wx.icardwx.utils.Constants;
import com.icard.wx.icardwx.utils.PropertyUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/home")
public class HomeController extends BaseController{

    @Autowired
    private IWxService wxService;

    @Autowired
    private IUserService userService;

    private List<FunctionModel> functionModelList = new ArrayList<>();

    @RequestMapping("")
    public String index(Model model, HttpServletRequest request, HttpServletResponse response){
        String code = request.getParameter("code");
        if(StringUtils.isBlank(code)){
            StringBuilder sb = new StringBuilder();
            sb.append(PropertyUtil.AUTH_URL).append(String.format("?appid=%s&redirect_uri=%s/home&response_type=code&scope=snsapi_base&state=1#wechat_redirect", PropertyUtil.getAppid(), PropertyUtil.getRedirectDomain()));
            return "redirect:"+sb.toString();
        }

        AccessToken accessToken =  wxService.getAccessToken(code);

        if(accessToken.getAccess_token()==null){
            StringBuilder sb = new StringBuilder();
            sb.append(PropertyUtil.AUTH_URL).append(String.format("?appid=%s&redirect_uri=%s/home&response_type=code&scope=snsapi_base&state=1#wechat_redirect", PropertyUtil.getAppid(), PropertyUtil.getRedirectDomain()));
            return "redirect:"+sb.toString();
        }

        String openId = accessToken.getOpenid();
        ResponseMessage msg = userService.getWxUserByOpenId(openId);
        if(msg.isSuccess()&&msg.getData()!=null){
            WxUser user = (WxUser) msg.getData();
            request.getSession().setAttribute(Constants.SESSION_USER,user);
            model.addAttribute("user",user);
            model.addAttribute("functionModels",getFunctionModelsByLevel(user.getLevel()));
        }else{
            //还没绑定
            model.addAttribute("openId",openId);
            return "wx_bind";
        }

        return "home";
    }

    private List<FunctionModel> getFunctionModelsByLevel(Integer level) {
        return functionModelList.stream().filter(x->x.getRequireLevelList().contains(level)).collect(Collectors.toList());
    }

    @PostConstruct
    public void init(){
        functionModelList.add(new FunctionModel("访客预约","reservation/list","fk.png", Arrays.asList(1,2)));
        functionModelList.add(new FunctionModel("水电查询","reservation/list","fk.png",Arrays.asList(0)));
    }
}
